﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using ShopOnline.BLL;

namespace WebUI.EditPages
{
    public partial class EditPaper : BasePage
    {
        CardPapers paper;
        int paperID;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            paperID = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
            if (mode == ADD_MODE)
            {
                btnSave.Enabled = false;
            }

            if (!IsPostBack && mode != ADD_MODE)
            {
                paper = new CardPapers();
                paper.LoadByPrimaryKey(paperID);
                txtName.Text = paper.Name;
                txtDescription.Text = paper.Description;
            }
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            Save();
        }

        private void Save()
        {
            paper = new CardPapers();
            if (mode == ADD_MODE)
            {
                paper.AddNew();
                paper.Deleted = false;
            }
            else
            {
                paper.LoadByPrimaryKey(paperID);
            }
            paper.Name = txtName.Text;
            paper.Description = txtDescription.Text;

            paper.Save();
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true); 
            ((Popup)Master).ShowMessage("Saved");
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            Save();
            CloseDlg(1);
        }
    }
}
